<?php namespace Environment;

use Illuminate\Console\Command;
//use Symfony\Component\Console\Input\InputOption;
//use Symfony\Component\Console\Input\InputArgument;

class EnvironmentCommand extends Command {

	/**
	 * The console command name.
	 *
	 * @var string
	 */
	protected $name = 'environment';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'Lists environment commands.';

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function fire()
	{
        $this->line("<command>environment:get</command> <info>gets the current host and environment.</info>");
        $this->line("<command>environment:set --host environment</command> <info>adds the current host name to the provided environment.</info>");
        $this->line("<command>environment:remove</command> <info>removes the current host name from the provided environment.</info>");
	}

	/**
	 * Get the console command arguments.
	 *
	 * @return array
	 */
	protected function getArguments()
	{
		return [];
	}

	/**
	 * Get the console command options.
	 *
	 * @return array
	 */
	protected function getOptions()
	{
		return [];
	}
	
	protected function getHost()
	{
		return gethostname();
	}
	
	protected function getEnvironment()
	{
		return \App::environment();
	}
	
	protected function getPath()
	{
		return app_path() . "/config/environment.php";
	}
	
	protected function getConfig()
	{
		$environments = require $this->getPath();
	
		if ( ! is_array($environments)) {
			$environments = [];
		}
	
		return $environments;
	}
	
 	protected function setConfig($config)
 	{
 		//$config = "<?php return " . var_export($config, true) . ";";
 		//File::put($this->getPath(), $config);
 		
		$code = "<?php\n\nreturn [";
	
		foreach ($config as $environment => $hosts)
		{
			$code .= "\n    \"" . $environment . "\" => [";
	
			foreach ($hosts as $host)
			{
				$code .= "\n        \"" . $host . "\",";
			}
	
			$code = trim($code, ",");
			$code .= "\n    ],";
		}
	
		$code = trim($code, ",");
		File::put($this->getPath(), $code . "\n];");
 	}

}
